package com.hyh.product.controller;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.hyh.product.domain.Product;
import com.hyh.product.result.PageResult;
import com.hyh.product.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


@RestController
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;


    /*
     * @description: 删除商品
     * @param:
     * @return:
     * @author
     * @date: 2024/3/20 14:48
     */
    @DeleteMapping("/{id}")
    public PageResult deleteProduct(@PathVariable("id") Long id) {
        return productService.deleteProduct(id);
    }

    /*
     * @description: 更新商品
     * @param:
     * @return:
     * @date: 2024/3/20 14:48
     */
    @PutMapping("/update/{id}")
    public PageResult updateProduct(@PathVariable("id") Long id) {
        return productService.updateProduct(id);
    }


    /*
     * @description: 更新库存
     * @param:
     * @return:
     * @author
     * @date: 2024/3/20 14:57
     */
    @PutMapping("/updateStock/{id}")
    public PageResult updateStock(@PathVariable("id") Long id,
                                  @RequestParam("stock") Integer stock) {
        return productService.updateStock(id, stock);
    }


    /*
     * @description: 根据商品id查询商品信息
     * @param:
     * @return:
     * @author
     * @date: 2024/3/21 13:58
     */
    @GetMapping("/{id}")
    public Product getProductById(@PathVariable("id") Long id) {
        return productService.getProductById(id);
    }


    /*
     * @description: 发布商品
     * @param:
     * @return:
     * @author
     * @date: 2024/3/25 15:51
     */
    @PostMapping("/publish" )
    public PageResult publishProduct(@JsonProperty Product product, @RequestParam("file")MultipartFile[] multipartFiles){
        return productService.publishProduct(product,multipartFiles);
    }


    /*
     * @description: 发布图片资源
     * @param:
     * @return:
     * @author
     * @date: 2024/3/25 16:11
     */
    @PostMapping("/publishImage")
    public PageResult publishImage(@RequestParam("file") MultipartFile[] file){
        return productService.publishImage(file);
    }

    // 查询所有商品
    @GetMapping("/all")
    public PageResult getAllProduct() {
        return productService.getAllProduct();
    }


    /**
     * @description 分页查询商品
     * @param
     * @return
     * @author
     * @date  2024/4/15 21:53
     */
    @GetMapping("/page")
    public PageResult getProductByPage(@RequestParam("page") Integer page,
                                       @RequestParam("size") Integer size) {
        return productService.getProductByPage(page, size);
    }
}
